{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "Chapter 05\n",
    "# 嵌套列表浅复制 vs 深复制\n",
    "Book_1《编程不难》 | 鸢尾花书：从加减乘除到机器学习"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "原始list\n",
      "[1, 2, 3, [4, 5]]\n"
     ]
    }
   ],
   "source": [
    "import copy\n",
    "\n",
    "list1 = [1, 2, 3, [4, 5]]\n",
    "print('原始list')\n",
    "print(list1)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-07-02T16:37:30.962591Z",
     "end_time": "2024-07-02T16:37:30.973053Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "outputs": [],
   "source": [
    "# 深复制，适用于嵌套列表\n",
    "list_deep = copy.deepcopy(list1)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-07-02T16:37:30.970818Z",
     "end_time": "2024-07-02T16:37:30.978580Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "outputs": [],
   "source": [
    "# 只深复制一层\n",
    "list2 = list1.copy()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-07-02T16:37:30.977587Z",
     "end_time": "2024-07-02T16:37:30.983423Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "outputs": [],
   "source": [
    "list3 = list1[:]"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-07-02T16:37:30.983423Z",
     "end_time": "2024-07-02T16:37:30.988178Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "outputs": [],
   "source": [
    "list4 = list(list1)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-07-02T16:37:30.990177Z",
     "end_time": "2024-07-02T16:37:30.994942Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "outputs": [],
   "source": [
    "list5 = [*list1]"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-07-02T16:37:30.994942Z",
     "end_time": "2024-07-02T16:37:31.000051Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "outputs": [],
   "source": [
    "# 修改元素\n",
    "list_deep[3][0] = 'deep'\n",
    "list_deep[2] = 'worked_0'"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-07-02T16:37:31.000051Z",
     "end_time": "2024-07-02T16:37:31.005163Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "outputs": [],
   "source": [
    "list2[3][0] = 'abc'\n",
    "list2[2] = 'worked_1'"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-07-02T16:37:31.007260Z",
     "end_time": "2024-07-02T16:37:31.054354Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "outputs": [],
   "source": [
    "list3[3][0] = 'X1'\n",
    "list3[2] = 'worked_2'"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-07-02T16:37:31.013370Z",
     "end_time": "2024-07-02T16:37:31.065354Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "outputs": [],
   "source": [
    "list4[3][0] = 'X2'\n",
    "list4[2] = 'worked_3'"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-07-02T16:37:31.016898Z",
     "end_time": "2024-07-02T16:37:31.066337Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "outputs": [],
   "source": [
    "list5[3][0] = 'X3'\n",
    "list5[2] = 'worked_4'"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-07-02T16:37:31.024261Z",
     "end_time": "2024-07-02T16:37:31.066337Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "新list\n",
      "[1, 2, 3, ['X3', 5]]\n",
      "[1, 2, 'worked_0', ['deep', 5]]\n"
     ]
    }
   ],
   "source": [
    "print('新list')\n",
    "print(list1)\n",
    "print(list_deep)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-07-02T16:37:31.028276Z",
     "end_time": "2024-07-02T16:37:31.066337Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 'worked_1', ['X3', 5]]\n",
      "[1, 2, 'worked_2', ['X3', 5]]\n",
      "[1, 2, 'worked_3', ['X3', 5]]\n",
      "[1, 2, 'worked_4', ['X3', 5]]\n"
     ]
    }
   ],
   "source": [
    "print(list2)\n",
    "print(list3)\n",
    "print(list4)\n",
    "print(list5)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-07-02T16:37:31.036048Z",
     "end_time": "2024-07-02T16:37:31.066337Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-07-02T16:37:31.041322Z",
     "end_time": "2024-07-02T16:37:31.066337Z"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
